package com.example.vehiclemanagesystem.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
	public static String currentDateTimeString() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return sdf.format(date);
	}

	public static String formatDateTime(java.sql.Date studytime) {
		if (studytime == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(new Date(studytime.getTime()));
	}

	public static String formatDateTime(Date studytime) {
		if (studytime == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(studytime.getTime());
	}

	public static String formatDate(java.sql.Date kc_time_limit) {
		if (kc_time_limit == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date(kc_time_limit.getTime()));
	}

	public static String formatDate(Date kc_time_limit) {
		if (kc_time_limit == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(kc_time_limit.getTime());
	}

	public static String formatIntegerToTime(Integer kc_time) {
		String time = "";
		int second = kc_time % 60;
		if (second < 10) {
			time = "0" + second;
		} else {
			time = "" + second;
		}
		kc_time = kc_time / 60;
		int minutes = kc_time % 60;
		if (minutes < 10) {
			time = "0" + minutes + ":" + time;
		} else {
			time = "" + minutes + ":" + time;
		}
		kc_time = kc_time / 60;
		int hour = kc_time % 60;
		if (hour < 10) {
			time = "0" + hour + ":" + time;
		} else {
			time = hour + ":" + time;
		}
		return time;
	}

	public static String formatIntegerToMinutesSecond(Integer kc_time) {
		String time = "";
		int second = kc_time % 60;
		if (second < 10) {
			time = "0" + second;
		} else {
			time = "" + second;
		}
		kc_time = kc_time / 60;
		int minutes = kc_time % 60;
		if (minutes < 10) {
			time = "0" + minutes + ":" + time;
		} else {
			time = "" + minutes + ":" + time;
		}
		kc_time = kc_time / 60;
		return time;
	}

	public static String formatIntegerToChineseTime(Integer kc_time) {
		String time = "";

		int second = kc_time % 60;

		kc_time = kc_time / 60;
		int minutes = kc_time % 60;

		kc_time = kc_time / 60;
		int hour = kc_time % 60;

		if (hour > 0) {
			time = hour + "小时";
		}
		if (minutes > 0) {
			time = time + minutes + "分钟";
		}
		if (second > 0) {
			time = time + second + "秒";
		}
		if (hour == 0 && minutes == 0 && second == 0) {
			time = "0秒";
		}
		return time;
	}

	public static java.sql.Date parseDateTime(String kcTimeLimit) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		if(kcTimeLimit == null){
			return null;
		}
		try {
			date = sdf.parse(kcTimeLimit);
		} catch (ParseException e) {
			//e.printStackTrace();
			return null;

		}
		if (date != null) {
			return new java.sql.Date(date.getTime());
		} else {
			return null;
		}
	}
	public static Date parseDateOnly(String dateTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(dateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (date != null) {
			return new Date(date.getTime());
		} else {
			return null;
		}
	}
	public static Date addDay(Date createtime, int i) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(createtime);
		calendar.add(Calendar.DAY_OF_MONTH, i);
		return calendar.getTime();
	}

	public static Object formatDateTimeFull(java.sql.Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(date.getTime()));
	}

	public static Object formatDateTimeFull(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date.getTime());
	}

	public static java.sql.Date parseDate(String dateTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = null;
		try {
			date = sdf.parse(dateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (date != null) {
			return new java.sql.Date(date.getTime());
		} else {
			return null;
		}
	}

	public static Date addYead(Date auth_start_time, int i) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(auth_start_time);
		calendar.add(Calendar.YEAR, i);
		return calendar.getTime();
	}


	/**
	 * 日期转字符串
	 *
	 * @param date    日期
	 * @param pattern 格式
	 * @return
	 */
	public static String dateToString(Date date, String pattern) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			return sdf.format(date);
		}
		return "";
	}


	public static int subDay(Date auth_end_time, Date date) {
		GregorianCalendar endCalendar = new GregorianCalendar();
		endCalendar.setTime(auth_end_time);
		endCalendar.set(Calendar.HOUR_OF_DAY, 0);
		endCalendar.set(Calendar.MINUTE, 0);
		endCalendar.set(Calendar.SECOND, 0);

		GregorianCalendar startCalendar = new GregorianCalendar();
		startCalendar.setTime(date);
		startCalendar.set(Calendar.HOUR_OF_DAY, 0);
		startCalendar.set(Calendar.MINUTE, 0);
		startCalendar.set(Calendar.SECOND, 0);

		int days = ((int) (endCalendar.getTime().getTime() / 1000) - (int) (startCalendar
				.getTime().getTime() / 1000)) / 3600 / 24;

		return days;
	}


	/**
	 * @param date1  字符串日期1
	 * @param date2  字符串日期2
	 * @param format 日期格式化方式  format="yyyy-MM-dd"
	 * @return
	 * @descript:计算两个字符串日期相差的天数
	 */
	public static long dayDiff(String date1, String date2, String format) {
		SimpleDateFormat formater = new SimpleDateFormat(format);
		long diff = 0l;
		try {
			long d1 = formater.parse(date1).getTime();
			long d2 = formater.parse(date2).getTime();
			//diff=(Math.abs(d1-d2) / (1000 * 60 * 60 * 24));
			diff = (d1 - d2) / (1000 * 60 * 60 * 24);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return diff;
	}
}
